package cn.lovemmd.model.admin.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import cn.lovemmd.model.admin.entity.DownloadLog;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Param;

import java.util.List;
import java.util.Map;

@Mapper
public interface DownloadLogMapper extends BaseMapper<DownloadLog> {

    @Select("SELECT COUNT(*) FROM model_download_log")
    long countAll();

    @Select("SELECT COUNT(*) FROM model_download_log WHERE created_at >= NOW() - INTERVAL 1 DAY")
    long countLast24h();

    @Select("SELECT COUNT(*) FROM model_download_log WHERE created_at >= NOW() - INTERVAL 7 DAY")
    long countLast7d();

    /**
     * 最近下载活动（联表模型与用户）
     */
    @Select("SELECT l.id, l.created_at, m.m_name AS model_name, u.u_name AS user_name " +
            "FROM model_download_log l " +
            "LEFT JOIN model_list m ON m.m_id = l.model_id " +
            "LEFT JOIN model_sys_user u ON u.u_id = l.user_id " +
            "ORDER BY l.created_at DESC LIMIT #{limit}")
    List<Map<String, Object>> selectRecentDownloads(@Param("limit") int limit);
}
